package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.UcenterMember;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {
    private EduCommentService eduCommentService;
    @Autowired
    public void setEduCommentService(EduCommentService eduCommentService) {
        this.eduCommentService = eduCommentService;
    }

    private UcenterClient ucenterClient;
    @Autowired
    public void setUcenterClient(UcenterClient ucenterClient) {
        this.ucenterClient = ucenterClient;
    }

    //根据课程id查询评论列表
    @ApiOperation(value = "评论分页列表")
    @GetMapping("/{courseId}/{page}/{limit}")
    public R index( @PathVariable Long page, @PathVariable Long limit,@PathVariable String courseId) {
        Page<EduComment> pageParam = new Page<>(page, limit);

        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);

        eduCommentService.page(pageParam,wrapper);
        List<EduComment> commentList = pageParam.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    // 添加评论
    @PostMapping("/save")
    public R saveComment(@RequestBody EduComment comment, HttpServletRequest request){
        // 获取token,判断用户是否登录
        String ucenterId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(ucenterId)){
            return R.error().code(28004).message("请登录");
        }

        System.out.println(ucenterId);

        // 根据远程接口调用来获取用户信息
        UcenterMember ucenterMember = ucenterClient.getInfo(ucenterId);

        // 封装信息
        comment.setMemberId(ucenterId);
        comment.setNickname(ucenterMember.getNickname());
        comment.setAvatar(ucenterMember.getAvatar());

        // 保存信息到数据库
        eduCommentService.save(comment);
        return R.ok();
    }
}

